﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using XiaoMan.BLL;
using XiaoMan.Model.Models;

namespace XiaoMan.API.Controllers
{
    [RoutePrefix("api/forumtopic")]
    public class Forum_TopicController : ApiBaseController
    {
        Sys_RelationService rs = new Sys_RelationService();
        Forum_TopicService fts = new Forum_TopicService();

        /// <summary>
        /// 根据用户ID查询关注的人的帖子
        /// </summary>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [Route("getforumtopicbyuser")]
        [HttpGet]
        public IHttpActionResult GetForum_TopicByUser(int? pageindex)
        {
            int userid = Convert.ToInt32(Request.Headers.GetValues("userid").FirstOrDefault());
            //拿到所有关注的人ID
            List<Sys_Relation> relations = rs.GetRelationByUser(userid);
            List<int> userids = new List<int>();
            foreach (var item in relations)
            {
                userids.Add(item.FollowUserID.Value);
            }
            List<Forum_Topic> Forum_Topics = fts.GetForum_TopicByUserid(userids, pageindex.Value);
            return ApiSuccess(Forum_Topics);
        }
        /// <summary>
        /// 根据圈子ID查询所有的帖子
        /// </summary>
        /// <param name="Disease"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [Route("getforumtopicbydisease")]
        [HttpGet]
        public IHttpActionResult GetForum_TopicByDisease(int? Disease, int? pageindex)
        {
            List<Forum_Topic> Forum_Topics = fts.GetForum_TopicByDisease(Disease.Value, pageindex.Value);
            return ApiSuccess(Forum_Topics);
        }

        /// <summary>
        /// 点赞或取消点赞
        /// </summary>
        /// <param name="TopicID">帖子ID</param>
        /// <param name="UpVoteType">点赞或取消点赞，1为点赞，0为取消点赞</param>
        /// <param name="UpVoteUserID">点赞人ID</param>
        [Route("postupvoteornot")]
        [HttpPost]
        public void postUpVoteOrNot(dynamic obj)
        {
            string TopicIDStr = Convert.ToString(obj.TopicID);
            string UpVoteTypeStr = Convert.ToString(obj.UpVoteType);
            string UpVoteUserIDStr = Convert.ToString(obj.UpVoteUserID);
            if (!string.IsNullOrEmpty(TopicIDStr) && !string.IsNullOrEmpty(UpVoteTypeStr) && !string.IsNullOrEmpty(UpVoteUserIDStr))
            {
                int TopicID = 0;
                int UpVoteType = 0;
                int UpVoteUserID = 0;
                if (int.TryParse(TopicIDStr, out TopicID) && TopicID > 0 && int.TryParse(UpVoteTypeStr, out UpVoteType) && (UpVoteType == 1 || UpVoteType == 0) && int.TryParse(UpVoteUserIDStr, out UpVoteUserID) && (UpVoteUserID > 1))
                {
                    Forum_Topic ForumTopic = fts.GetList(f => f.TopicID == TopicID).FirstOrDefault();
                    if (ForumTopic != null)
                    {
                        if (UpVoteType == 1)
                        {
                            ForumTopic.UpVoteCount = ForumTopic.UpVoteCount++;
                        }
                        else
                        {
                            ForumTopic.UpVoteCount = ForumTopic.UpVoteCount--;
                        }
                        fts.Update(ForumTopic);
                    }
                }
            }
        }

        /// <summary>
        /// 收藏或取消收藏
        /// </summary>
        /// <param name="TopicID">帖子ID</param>
        /// <param name="CollectType">收藏或取消收藏，1为收藏，0为取消收藏</param>
        /// <param name="CollectUserID">收藏人ID</param>
        [Route("postcollectornot")]
        [HttpPost]
        public void postCollectOrNot(dynamic obj)
        {
            string TopicIDStr = Convert.ToString(obj.TopicID);
            string CollectTypeStr = Convert.ToString(obj.CollectType);
            string CollectUserIDStr = Convert.ToString(obj.CollectUserID);
            if (!string.IsNullOrEmpty(TopicIDStr) && !string.IsNullOrEmpty(CollectTypeStr) && !string.IsNullOrEmpty(CollectUserIDStr))
            {
                int TopicID = 0;
                int CollectType = 0;
                int CollectUserID = 0;
                if (int.TryParse(TopicIDStr, out TopicID) && TopicID > 0 && int.TryParse(CollectTypeStr, out CollectType) && (CollectType == 1 || CollectType == 0) && int.TryParse(CollectUserIDStr, out CollectUserID) && (CollectUserID > 1))
                {
                    Forum_Topic ForumTopic = fts.GetList(f => f.TopicID == TopicID).FirstOrDefault();
                    if (ForumTopic != null)
                    {
                        if (CollectType == 1)
                        {
                            ForumTopic.CollectCount = ForumTopic.CollectCount++;
                        }
                        else
                        {
                            ForumTopic.CollectCount = ForumTopic.CollectCount--;
                        }
                        fts.Update(ForumTopic);
                    }
                }
            }
        }

        /// <summary>
        /// 设置是否为精华帖
        /// </summary>
        /// <param name="TopicID">帖子ID</param>
        /// <param name="EssentialType">是否为精华帖，1为设置为精华帖，0为设置为不是精华帖</param>
        [Route("postisessential")]
        [HttpPost]
        public void postIsEssential(dynamic obj)
        {
            string TopicIDStr = Convert.ToString(obj.TopicID);
            string EssentialTypeStr = Convert.ToString(obj.EssentialType);
            if (!string.IsNullOrEmpty(TopicIDStr) && !string.IsNullOrEmpty(EssentialTypeStr))
            {
                int TopicID = 0;
                int EssentialType = 0;
                if (int.TryParse(TopicIDStr, out TopicID) && TopicID > 0 && int.TryParse(EssentialTypeStr, out EssentialType) && (EssentialType == 1 || EssentialType == 0))
                {
                    Forum_Topic ForumTopic = fts.GetList(f => f.TopicID == TopicID).FirstOrDefault();
                    if (ForumTopic != null)
                    {
                        if (EssentialType == 1)
                        {
                            ForumTopic.IsEssential = true;
                        }
                        else
                        {
                            ForumTopic.IsEssential = false;
                        }
                        fts.Update(ForumTopic);
                    }
                }
            }
        }

        /// <summary>
        /// 设置用户个人资料界面置顶帖子
        /// </summary>
        /// <param name="TopicID">帖子ID</param>
        /// <param name="TopType">是否置顶，1为设置为置顶，0为设置为不置顶</param>
        [Route("postistopforsingleuser")]
        [HttpPost]
        public void postIsTopForSingleUser(dynamic obj)
        {
            string TopicIDStr = Convert.ToString(obj.TopicID);
            string TopTypeStr = Convert.ToString(obj.TopType);
            if (!string.IsNullOrEmpty(TopicIDStr) && !string.IsNullOrEmpty(TopTypeStr))
            {
                int TopicID = 0;
                int TopType = 0;
                if (int.TryParse(TopicIDStr, out TopicID) && TopicID > 0 && int.TryParse(TopTypeStr, out TopType) && (TopType == 1 || TopType == 0))
                {
                    Forum_Topic ForumTopic = fts.GetList(f => f.TopicID == TopicID).FirstOrDefault();
                    if (ForumTopic != null)
                    {
                        if (TopType == 1)
                        {
                            ForumTopic.IsTopForSingleUser = true;
                        }
                        else
                        {
                            ForumTopic.IsTopForSingleUser = false;
                        }
                        fts.Update(ForumTopic);
                    }
                }
            }
        }

        /// <summary>
        /// 设置疾病圈子置顶帖子
        /// </summary>
        /// <param name="TopicID">帖子ID</param>
        /// <param name="TopType">是否置顶，1为设置为置顶，0为设置为不置顶</param>
        [Route("postisdiseasetop")]
        [HttpPost]
        public void postIsDiseaseTop(dynamic obj)
        {
            string TopicIDStr = Convert.ToString(obj.TopicID);
            string TopTypeStr = Convert.ToString(obj.TopType);
            if (!string.IsNullOrEmpty(TopicIDStr) && !string.IsNullOrEmpty(TopTypeStr))
            {
                int TopicID = 0;
                int TopType = 0;
                if (int.TryParse(TopicIDStr, out TopicID) && TopicID > 0 && int.TryParse(TopTypeStr, out TopType) && (TopType == 1 || TopType == 0))
                {
                    Forum_Topic ForumTopic = fts.GetList(f => f.TopicID == TopicID).FirstOrDefault();
                    if (ForumTopic != null)
                    {
                        if (TopType == 1)
                        {
                            ForumTopic.IsTop = true;
                        }
                        else
                        {
                            ForumTopic.IsTop = false;
                        }
                        fts.Update(ForumTopic);
                    }
                }
            }
        }
    }
}